package cn.myzf.chatapi.wechat.utils;

import cn.myzf.chatapi.WeChatClient;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;

/**
 *
 * 注册掉线监听器
 * @package cn.myzf.chatapi.wechat.utils
 * @Date Created in 2019/7/30 17:58
 * @Author myzf
 */
@Component
public class MyApplicationRunner implements ApplicationRunner {
     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Override
    public void run(ApplicationArguments var1) throws Exception{

        WeChatClient.getInstance().addLoginChangedListener(new WeChatClient.LoginChangedListener() {
            @Override
            public void onChanged() {
                WeChatClient weChatClient = WeChatClient.getInstance();
                if(!WeChatClient.isLogin()){
                    //销毁线程
                    weChatClient = null;
                    WxClientRel.remove("myzf",weChatClient);

                }else {
                    WxClientRel.put("myzf",weChatClient);
                }

            }
        });


    }
}
